/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai;

import ORG.oclc.oai.server.catalog.RecordFactory;
import ORG.oclc.oai.server.verb.CannotDisseminateFormatException;
import cz.mzk.oai.metadataRepository.repository.collections.Item;
import cz.mzk.oai.metadataRepository.repository.OaiSet;
import cz.mzk.oai.metadataRepository.repository.Repository;
import java.util.Iterator;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
//http://pubserv.oclc.org/oaicat/jars/docs/README.txt

/**
 * XMLRecordFactory converts native XML "items" to "record" Strings.
 * This factory assumes the native XML item looks exactly like the <record>
 * element of an OAI GetRecord response, with the possible exception that the
 * <metadata> element contains multiple metadataFormats from which to choose.
 */
public class MyRecordFactory extends RecordFactory {

    private static final String identifierStart = "<identifier>";
    private static final String identifierEnd = "</identifier>";
    private static final String datestampStart = "<datestamp>";
    private static final String datestampEnd = "</datestamp>";
    private static final String setSpecStart = "<setSpec>";
    private static final String setSpecEnd = "</setSpec>";
    private static final String aboutStart = "<about>";
    private static final String aboutEnd = "</about>";
    private Repository repository;

    /**
     * Construct an XMLRecordFactory capable of producing the Crosswalk(s)
     * specified in the properties file.
     * @param properties Contains information to configure the factory:
     *                   specifically, the names of the crosswalk(s) supported
     * @exception IllegalArgumentException Something is wrong with the argument.
     */
    public MyRecordFactory(Properties properties) throws IllegalArgumentException {
        super(properties);
        repository = Repository.instanceOf(properties);
    }

    /**
     * Utility method to parse the 'local identifier' from the OAI identifier
     *
     * @param identifier OAI identifier (e.g. oai:oaicat.oclc.org:ID/12345)
     * @return local identifier (e.g. ID/12345).
     */
    public String fromOAIIdentifier(String identifier) {
        return identifier;
    }

    /**
     * Construct an OAI identifier from the native item
     *
     * @param nativeItem native Item object
     * @return OAI identifier
     */
    public String getOAIIdentifier(Object nativeItem) {
        Item item = (Item) nativeItem;
        return item.getId();
    }

    /**
     * get the datestamp from the item
     *
     * @param nativeItem a native item presumably containing a datestamp somewhere
     * @return a String containing the datestamp for the item
     * @exception IllegalArgumentException Something is wrong with the argument.
     */
    public String getDatestamp(Object nativeItem)
            throws IllegalArgumentException {
        Item item = (Item) nativeItem;
        return item.getDateStamp().toString();
    }

    /**
     * get the setspec from the item
     *
     * @param nativeItem a native item presumably containing a setspec somewhere
     * @return a String containing the setspec for the item
     * @exception IllegalArgumentException Something is wrong with the argument.
     */
    public Iterator getSetSpecs(Object nativeItem) throws IllegalArgumentException {
        /*  ArrayList list = new ArrayList();
        String xmlRec = (String) nativeItem;
        for (int startOffset = xmlRec.indexOf(setSpecStart);
        startOffset >= 0;
        startOffset = xmlRec.indexOf(setSpecStart, startOffset + 1)) {
        int endOffset = xmlRec.indexOf(setSpecEnd, startOffset + 1);
        list.add(xmlRec.substring(startOffset + setSpecStart.length(), endOffset));
        }
        return list.iterator();*/
        return null;//TODO
    }

    /**
     * Get the about elements from the item
     *
     * @param nativeItem a native item presumably containing about information somewhere
     * @return a Iterator of Strings containing &lt;about&gt;s for the item
     * @exception IllegalArgumentException Something is wrong with the argument.
     */
    public Iterator getAbouts(Object nativeItem) throws IllegalArgumentException {
        /* ArrayList list = new ArrayList();
        String xmlRec = (String) nativeItem;
        for (int startOffset = xmlRec.indexOf(aboutStart);
        startOffset >= 0;
        startOffset = xmlRec.indexOf(aboutStart, startOffset + 1)) {
        int endOffset = xmlRec.indexOf(aboutEnd, startOffset + 1);
        list.add(xmlRec.substring(startOffset + aboutStart.length(), endOffset));
        }
        return list.iterator();*/
        return null; //TODO
    }

    /**
     * Is the record deleted?
     *
     * @param nativeItem a native item presumably containing a possible delete indicator
     * @return true if record is deleted, false if not
     * @exception IllegalArgumentException Something is wrong with the argument.
     */
    public boolean isDeleted(Object nativeItem) throws IllegalArgumentException {
        /*String xmlRec = (String) nativeItem;
        return xmlRec.indexOf("<header status=\"deleted\"") != -1;*/
        return false;
        //TODO: dodelat
    }

    /**
     * Allows classes that implement RecordFactory to override the default create() method.
     * This is useful, for example, if the entire &lt;record&gt; is already packaged as the native
     * record. Return null if you want the default handler to create it by calling the methods
     * above individually.
     *
     * @param nativeItem the native record
     * @param schemaURL the schemaURL desired for the response
     * @param the metadataPrefix from the request
     * @return a String containing the OAI &lt;record&gt; or null if the default method should be
     * used.
     */
    public String quickCreate(Object nativeItem, String schemaUrl, String metadataPrefix) throws CannotDisseminateFormatException {
        /*Crosswalks crosswalks = this.getCrosswalks();
        if(!crosswalks.containsValue(metadataPrefix)){
        throw new CannotDisseminateFormatException("no such metadata prefix");
        }*/
        return null;
    }

    @Override
    public String[] createHeader(Object nativeItem) {
        String[] result = new String[2];
        Item item = (Item) nativeItem;
        Document doc = DocumentHelper.createDocument();
        Element headerEl = doc.addElement("header");
        Element identifierEl = headerEl.addElement("identifier");
        identifierEl.addText(item.getId());
        Element dateEl = headerEl.addElement("datestamp");
        dateEl.addText(item.getDateStampStr());
        for (OaiSet set : item.getCollection().getOaiSets()) {
            Element setSpecEl = headerEl.addElement("setSpec");
            setSpecEl.addText(set.getSetSpec());
        }
        result[0] = headerEl.asXML();
        result[1] = item.getId();
        return result;
    }
}

